<?php

/**
 * 菜单
 *
 * @author dalei.chen
 */

require_once 'CommonController.php';

class Admin_MenuController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_Menu();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $templateContent = file_get_contents(APPLICATION_PATH . '/configs/menuTemplate.php');
        
        $this->view->listContent = $this->_model->makeChildrenListString(0, $templateContent);
    }

    /**
     * 添加动作
     * 
     * @return void
     */
    public function insertAction()
    {
        $parentId = intval($this->_request->get('parentId'));
        if (!empty($parentId)) {
            $parentRow = $this->_model->fetchRow("`id` = {$parentId}"); 
        } else {
            $parentRow = array(); 
        }
        $this->view->parentRow = $parentRow;

        if ($this->_request->isPost()) {
            $time = time();
            $dataNum = intval($this->_request->getPost('dataNum'));
            $filter = new Zend_Filter_StripTags();
            $insertNum = 0;
            for($i = 0; $i <= $dataNum - 1; $i++) {
                $field = array();

                $field['name'] = $filter->filter(trim($this->_request->getPost('name' . $i)));
                $field['module'] = $filter->filter(trim($this->_request->getPost('module' . $i)));
                $field['module'] = str_replace('.', '-', $field['module']);
                $field['controller'] = $filter->filter(trim($this->_request->getPost('controller' . $i)));
                $field['controller'] = str_replace('.', '-', $field['controller']);
                $field['action'] = $filter->filter(trim($this->_request->getPost('action' . $i)));
                $field['action'] = str_replace('.', '-', $field['action']);
                $field['isLink'] = $this->_request->getPost('isLink' . $i) != NULL ? 1 : 0;;
                $field['otherQueryString'] = $filter->filter(trim($this->_request->getPost('otherQueryString' . $i)));
                $field['isDisplay'] = $this->_request->getPost('isDisplay' . $i) != NULL ? 1 : 0;
                $field['type'] = $this->_request->getPost('type' . $i);
                $field['status'] = $this->_request->getPost('status' . $i) != NULL ? 1 : 2;
                $field['sort'] = intval($this->_request->getPost('sort' . $i));
                $field['notes'] = $filter->filter(trim($this->_request->getPost('notes' . $i)));
                $field['helpUrl'] = $filter->filter(trim($this->_request->getPost('helpUrl'.$i)));
                $field['addTime']= date('Y-m-d H:i:s', $time);
                $field['parentId'] = $parentId;
                $field['parentIds'] = !empty($parentRow) ? $parentRow['parentIds'] . ',' . $parentId : 0;

                if ($field['name'] != '') {
                    $this->_model->insert($field);
                    $insertNum++;
                }
            }
            echo $this->view->message("操作成功，共保存{$insertNum}条记录。", $this->view->projectUrl(array('action'=>'index'))) ;
            exit;
        }
    }

    /**
     * 修改动作
     * 
     * @return void
     */
    public function updateAction()
    {
        $id = intval($this->_request->get('id'));
        if (empty($id)) {
             exit('Invalid params!');
        }
        $row = $this->_model->fetchRow("`id` = {$id}");
        $this->view->row = $row;

        if ($this->_request->isPost()) {
            $filter = new Zend_Filter_StripTags();
            $field = array();
            $time = time();

            $field['name'] = $filter->filter(trim($this->_request->getPost('name')));
            $field['module'] = $filter->filter(trim($this->_request->getPost('module')));
            $field['module'] = str_replace('.', '-', $field['module']);
            $field['controller'] = $filter->filter(trim($this->_request->getPost('controller')));
            $field['controller'] = str_replace('.', '-', $field['controller']);
            $field['action'] = $filter->filter(trim($this->_request->getPost('action')));
            $field['action'] = str_replace('.', '-', $field['action']);
            $field['isLink'] = $this->_request->getPost('isLink') != NULL ? 1 : 0;
            $field['otherQueryString'] = $filter->filter(trim($this->_request->getPost('otherQueryString')));
            $field['isDisplay'] = $this->_request->getPost('isDisplay') != NULL ? 1 : 0;
            $field['type'] = $this->_request->getPost('type');
            $field['status'] = $this->_request->getPost('status') != NULL ? 1 : 2;
            $field['sort'] = intval($this->_request->getPost('sort'));
            $field['notes'] = $filter->filter(trim($this->_request->getPost('notes')));
            $field['helpUrl'] = $filter->filter(trim($this->_request->getPost('helpUrl')));
            if ($row['name'] == '') {
                echo $this->view->message('名称不能为空，请返回填写！') ;
                exit;
            }
            $this->_model->update($field, "`id` = {$id}");
            echo $this->view->message("操作成功。", $this->view->projectUrl(array('action'=>'index'))) ;
            exit;
        }
    }

    /**
     * 删除动作
     * 
     * @return void
     */
    public function deleteAction()
    {
        if (is_string($this->_request->get('id'))) {
            $id = intval($this->_request->get('id'));
            $this->_model->delete($id);
        } else if ($this->_request->isPost()) {
            $ids = $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_model->deleteByIds($ids);
            }
        }
        redirect($this->view->projectUrl(array('action'=>'index')));
        exit;
    }

    /**
     * 排序动作
     * 
     * @return void
     */
    public function sortAction()
    {
        $ids = $this->_request->getPost('ids');
        $sort = $this->_request->getPost('sort');
        if (is_array($ids) && is_array($sort)) {
            foreach($ids as $key=>$id) {
                 $this->_model->update(array('sort'=>$sort[$key]), "`id` = {$id}");
            }
        }
        redirect($this->view->projectUrl(array('action'=>'index')));
        exit;
    }
}